package leetcode_201_300;

import java.util.Arrays;

public class LeeCode_2217 {
    public static void main(String[] args) {
//        System.out.println(Arrays.toString(kthPalindrome(
//                new int[]{289517270,867722538,877647319,64,33,10,532848524,48,101799647,96}, 5)));
//        System.out.println(Arrays.toString(kthPalindrome(
//                new int[]{8}, 5)));
        System.out.println(Arrays.toString(kthPalindrome(
                new int[]{27,91,58861714,54,671768337,91,11278338,27,8,226681966,96,931246031}, 5)));
//        System.out.println(Arrays.toString(kthPalindrome(new int[]{1, 2, 3, 4, 5, 90}, 3)));
//        System.out.println(Arrays.toString(kthPalindrome(new int[]{2,4,6}, 4)));
//        System.out.println(Arrays.toString(kthPalindrome(new int[]{2,4,90,91,89}, 4)));
//        System.out.println(Arrays.toString(kthPalindrome(new int[]{449229674,501930675,40059525,908875541,9,672504016}, 5)));
    }
    private static long[] kthPalindrome(int[] queries, int intLength) {
        int len = (intLength - 1) / 2, n = queries.length;
        long[] ans = new long[n];
        long tmp = (long) Math.pow(10, len);
        for (int i = 0; i < n; i++) {
            int q = queries[i];
            String s = Long.toString(tmp + q - 1);
            if (s.length() > (intLength + 1) / 2) {
                ans[i] = -1;
                continue;
            }
            StringBuilder sb = new StringBuilder(s);
            String pre = sb.toString();
            if (intLength % 2 == 1){
                sb.replace(sb.length() - 1, sb.length(), "");
            }
            ans[i] = Long.parseLong(pre + sb.reverse());
        }
        return ans;
    }
}
